home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_UDELxntp.idb / usr / freeware / src / xntp-3.4o-export / include / ntp.h.z / ntp.h
C/C++ Source or Header  |  1998-01-21  |  27KB  |  717 lines

  1. /*
  2.  * ntp.h - NTP definitions for the masses
  3.  */
  4.  
  5. #include "ntp_types.h"
  6.  
  7.  
  8. /*
  9.  * How to get signed characters.  On machines where signed char works,
  10.  * use it.  On machines where signed char doesn't work, char had better
  11.  * be signed.
  12.  */
  13. #if !defined(S_CHAR_DEFINED)
  14. #if defined(NO_SIGNED_CHAR_DECL)
  15. typedef char s_char;
  16. #else
  17. typedef signed char s_char;
  18. #endif
  19. #ifdef sequent
  20. #undef SO_RCVBUF
  21. #undef SO_SNDBUF
  22. #endif
  23. #endif
  24. #ifndef TRUE
  25. #define TRUE 1
  26. #endif /* TRUE */
  27. #ifndef FALSE
  28. #define FALSE 0
  29. #endif /* FALSE */
  30.  
  31. /*
  32.  * NTP protocol parameters.  See section 3.2.6 of the specification.
  33.  */
  34. #define    NTP_VERSION    ((u_char)3) /* current version number */
  35. #define    NTP_OLDVERSION    ((u_char)1) /* oldest credible version */
  36. #define    NTP_PORT    123    /* included for sake of non-unix machines */
  37. #define    NTP_MAXSTRATUM    ((u_char)15) /* max stratum, infinity a la Bellman-Ford */
  38. #define    NTP_MAXAGE    86400    /* one day in seconds */
  39. #define    NTP_MAXSKEW    1    /* 1 sec, skew after NTP_MAXAGE w/o updates */
  40. #define    NTP_SKEWINC    49170    /* skew increment for clock updates (l_f) */
  41. #define    NTP_SKEWFACTOR    16    /* approximation of factor for peer calcs */
  42. #define    NTP_MAXDISTANCE (1 * FP_SECOND) /* max. rootdelay for synchr. */
  43. #define NTP_MINDPOLL    6    /* log2 default min poll interval (64 s) */
  44. #define NTP_MAXDPOLL    10    /* log2 default max poll interval (~17 m) */
  45. #define    NTP_MINPOLL    4    /* log2 min poll interval (16 s) */
  46. #define    NTP_MAXPOLL    14    /* log2 max poll interval (~4.5 h) */
  47. #define    NTP_MINCLOCK    3    /* minimum for outlyer detection */
  48. #define    NTP_MAXCLOCK    10    /* maximum select list size */
  49. #define    NTP_MINDISPERSE    (FP_SECOND / 100) /* min dispersion (u_fp 10 ms) */
  50. #define    NTP_MAXDISPERSE    (FP_SECOND * 16) /* max dispersion (u_fp 16 s) */
  51. #define    NTP_DISPFACTOR    20    /* MAXDISPERSE as a shift (u_fp 16 s) */
  52. #define    NTP_WINDOW    8    /* reachability register size */
  53. #define    NTP_SHIFT    8    /* 8 suitable for crystal time base */
  54. #define    NTP_MAXKEY    65535    /* maximum authentication key number */
  55. #define NTP_MAXD    3    /* log2 estimated error averaging factor */
  56.  
  57. /*
  58.  * Loop filter parameters.  See section 5.1 of the specification.
  59.  *
  60.  * Note that these are appropriate for a crystal time base.  If your
  61.  * system clock is line frequency controlled you should read the
  62.  * specification for appropriate modifications.  Note that the
  63.  * loop filter code will have to change if you change CLOCK_MAX
  64.  * to be greater than or equal to 500 ms.
  65.  *
  66.  * Note these parameters have been rescaled for a time constant range from
  67.  * 0 through 10, with 2 corresoponding to the old time constant of 0.
  68.  */
  69. #define    CLOCK_MINSTEP    900    /* step timeout (sec) */
  70. #define    CLOCK_ADJ    0    /* log2 adjustment interval (1 sec) */
  71. #define    CLOCK_DSCALE    20    /* skew reg. scale: unit is 2**-20 ~= 1 ppm */
  72. #define    CLOCK_FREQ    16    /* log2 frequency weight (65536) */
  73. #define    CLOCK_PHASE    6    /* log2 phase weight (64) */
  74. #define CLOCK_LIMIT    30    /* time constant adjust threshold */
  75. #define CLOCK_G        2    /* log2 frequency averaging factor */
  76. #define CLOCK_MAXSEC    800    /* max update interval for pll */
  77.  
  78. #define CLOCK_MAX_FP    0x000020c5 /* max clock offset (s_fp 128 ms) */
  79. #define    CLOCK_MAX_F    0x20c49ba6 /* max clock offset (l_fp 128 ms) */
  80. #define    CLOCK_MAX_I    0x00000000 /* both fractional and integral parts */
  81.  
  82. #define    CLOCK_WAYTOOBIG    1000    /* if clock 1000 sec off, forget it */
  83.  
  84. /*
  85.  * Event timers are actually implemented as a sorted queue of expiry
  86.  * times.  The queue is slotted, with each slot holding timers which
  87.  * expire in a 2**(NTP_MINPOLL-1) (8) second period.  The timers in
  88.  * each slot are sorted by increasing expiry time.  The number of
  89.  * slots is 2**(NTP_MAXPOLL-(NTP_MINPOLL-1)), or 512, to cover a time
  90.  * period of 2**NTP_MAXPOLL (16384) seconds into the future before
  91.  * wrapping.
  92.  */
  93. #define    EVENT_TIMEOUT    CLOCK_ADJ
  94.  
  95. struct event {
  96.     struct event *next;        /* next in chain */
  97.     struct event *prev;        /* previous in chain */
  98.     struct peer *peer;        /* peer this counter belongs to */
  99.     void (*event_handler)();    /* routine to call to handle event */
  100.     u_long event_time;        /* expiry time of counter */
  101. };
  102.  
  103. #define    TIMER_SLOTTIME    (1<<(NTP_MINPOLL-1))
  104. #define    TIMER_NSLOTS    (1<<(NTP_MAXPOLL-(NTP_MINPOLL-1)))
  105. #define    TIMER_SLOT(t)    (((t) >> (NTP_MINPOLL-1)) & (TIMER_NSLOTS-1))
  106.  
  107. /*
  108.  * TIMER_ENQUEUE() puts stuff on the timer queue.  It takes as
  109.  * arguments (ea), an array of event slots, and (iev), the event
  110.  * to be inserted.  This one searches the hash bucket from the
  111.  * end, and is about optimum for the timing requirements of
  112.  * NTP peers.
  113.  */
  114. #define    TIMER_ENQUEUE(ea, iev) \
  115.     do { \
  116.         register struct event *ev; \
  117.         \
  118.         ev = (ea)[TIMER_SLOT((iev)->event_time)].prev; \
  119.         while (ev->event_time > (iev)->event_time) \
  120.             ev = ev->prev; \
  121.         (iev)->prev = ev; \
  122.         (iev)->next = ev->next; \
  123.         (ev)->next->prev = (iev); \
  124.         (ev)->next = (iev); \
  125.     } while(0)
  126.  
  127. /*
  128.  * TIMER_INSERT() also puts stuff on the timer queue, but searches the
  129.  * bucket from the top.  This is better for things that do very short
  130.  * time outs, like clock support.
  131.  */
  132. #define    TIMER_INSERT(ea, iev) \
  133.     do { \
  134.         register struct event *ev; \
  135.         \
  136.         ev = (ea)[TIMER_SLOT((iev)->event_time)].next; \
  137.         while (ev->event_time != 0 && \
  138.             ev->event_time < (iev)->event_time) \
  139.             ev = ev->next; \
  140.         (iev)->next = ev; \
  141.         (iev)->prev = ev->prev; \
  142.         (ev)->prev->next = (iev); \
  143.         (ev)->prev = (iev); \
  144.     } while(0)
  145.  
  146. /*
  147.  * Remove an event from the queue.
  148.  */
  149. #define    TIMER_DEQUEUE(ev) \
  150.     do { \
  151.         if ((ev)->next != 0) { \
  152.             (ev)->next->prev = (ev)->prev; \
  153.             (ev)->prev->next = (ev)->next; \
  154.             (ev)->next = (ev)->prev = 0; \
  155.         } \
  156.     } while (0)
  157.  
  158. /*
  159.  * The interface structure is used to hold the addresses and socket
  160.  * numbers of each of the interfaces we are using.
  161.  */
  162. struct interface {
  163.     int fd;            /* socket this is opened on */
  164.     int bfd;        /* socket for receiving broadcasts */
  165.     struct sockaddr_in sin;    /* interface address */
  166.     struct sockaddr_in bcast;    /* broadcast address */
  167.     struct sockaddr_in mask;    /* interface mask */
  168.     char name[8];        /* name of interface */
  169.     int flags;        /* interface flags */
  170.     int last_ttl;        /* last TTL specified */
  171.     long received;        /* number of incoming packets */
  172.     long sent;        /* number of outgoing packets */
  173.     long notsent;        /* number of send failures */
  174. };
  175.  
  176. /*
  177.  * Flags for interfaces
  178.  */
  179. #define    INT_BROADCAST    1    /* can broadcast out this interface */
  180. #define    INT_BCASTOPEN    2    /* broadcast socket is open */
  181. #define    INT_LOOPBACK    4    /* the loopback interface */
  182. #define INT_MULTICAST    8    /* multicasting enabled */
  183.  
  184. /*
  185.  * Define flasher bits (tests 1 through 8 in packet procedure)
  186.  * These reveal the state at the last grumble from the peer and are
  187.  * most handy for diagnosing problems, even if not strictly a state
  188.  * variable in the spec. These are recorded in the peer structure.
  189.  */
  190. #define TEST1        0x01    /* duplicate packet received */
  191. #define TEST2        0x02    /* bogus packet received */
  192. #define TEST3        0x04    /* protocol unsynchronized */
  193. #define TEST4        0x08    /* peer delay/dispersion bounds check */
  194. #define TEST5        0x10    /* peer authentication failed */
  195. #define TEST6        0x20    /* peer clock unsynchronized */
  196. #define TEST7        0x40    /* peer stratum out of bounds */
  197. #define TEST8        0x80    /* root delay/dispersion bounds check */
  198.  
  199. /*
  200.  * The peer structure.  Holds state information relating to the guys
  201.  * we are peering with.  Most of this stuff is from section 3.2 of the
  202.  * spec.
  203.  */
  204. struct peer {
  205.     struct peer *next;
  206.     struct peer *ass_next;        /* link pointer in associd hash */
  207.     struct sockaddr_in srcadr;    /* address of remote host */
  208.     struct interface *dstadr;    /* pointer to address on local host */
  209.     struct refclockproc *procptr;    /* pointer to reference clock sutuff */
  210.     u_char leap;            /* leap indicator */
  211.     u_char hmode;            /* association mode with this peer */
  212.     u_char pmode;            /* peer's association mode */
  213.     u_char stratum;            /* stratum of remote peer */
  214.     s_char precision;        /* peer's clock precision */
  215.     u_char ppoll;            /* peer poll interval */
  216.     u_char hpoll;            /* local host poll interval */
  217.     u_char minpoll;            /* min local host poll interval */
  218.     u_char maxpoll;            /* max local host poll interval */
  219.     u_char version;            /* version number */
  220.     u_char flags;            /* peer flags */
  221.     u_char cast_flags;        /* flags MDF_?CAST */
  222.     u_char flash;            /* peer flashers (for maint) */
  223.     u_char refclktype;        /* reference clock type */
  224.     u_char refclkunit;        /* reference clock unit number */
  225.     u_char sstclktype;        /* clock type for system status word */
  226.     s_fp rootdelay;            /* distance from primary clock */
  227.     u_fp rootdispersion;        /* peer clock dispersion */
  228.     u_int32 refid;            /* peer reference ID */
  229.     l_fp reftime;            /* time of peer's last update */
  230.     struct event event_timer;    /* event queue entry */
  231.     u_int32 keyid;            /* encription key ID */
  232.     u_int32 pkeyid;            /* keyid used to encrypt last message */
  233.     u_short associd;        /* association ID, a unique integer */
  234.     u_char ttl;            /* time to live (multicast) */
  235. /* **Start of clear-to-zero area.*** */
  236. /* Everything that is cleared to zero goes below here */
  237.     u_char valid;            /* valid counter */
  238. #define    clear_to_zero    valid
  239.     u_char reach;            /* reachability, NTP_WINDOW bits */
  240.     u_char unreach;            /* unreachable count */
  241.     u_short filter_nextpt;        /* index into filter shift register */
  242.     s_fp filter_delay[NTP_SHIFT];    /* delay part of shift register */
  243.     l_fp filter_offset[NTP_SHIFT];    /* offset part of shift register */
  244.     s_fp filter_soffset[NTP_SHIFT]; /* offset in s_fp format, for disp */
  245.     l_fp org;            /* originate time stamp */
  246.     l_fp rec;            /* receive time stamp */
  247.     l_fp xmt;            /* transmit time stamp */
  248. /* ***End of clear-to-zero area.*** */
  249. /* Everything that is cleared to zero goes above here */
  250.     u_char filter_order[NTP_SHIFT]; /* we keep the filter sorted here */
  251. #define    end_clear_to_zero    filter_order[0]
  252.     u_fp filter_error[NTP_SHIFT];    /* error part of shift register */
  253.     long update;            /* base sys_clock for skew calc.s */
  254.     s_fp delay;            /* filter estimated delay */
  255.     u_fp dispersion;        /* filter estimated dispersion */
  256.     l_fp offset;            /* filter estimated clock offset */
  257.     s_fp soffset;            /* fp version of above */
  258.     s_fp synch;            /* synch distance from above */
  259.     u_fp selectdisp;        /* select dispersion */
  260.     s_fp estbdelay;            /* broadcast offset */
  261.  
  262.     /*
  263.      * statistic counters
  264.      */
  265.     u_long timereset;        /* time stat counters were reset */
  266.     u_long sent;            /* number of updates sent */
  267.     u_long received;        /* number of frames received */
  268.     u_long timereceived;        /* last time a frame received */
  269.     u_long timereachable;        /* last reachable/unreachable event */
  270.     u_long processed;        /* processed by the protocol */
  271.     u_long badauth;            /* bad credentials detected */
  272.     u_long bogusorg;        /* rejected due to bogus origin */
  273.     u_long oldpkt;            /* rejected as duplicate packet */
  274.     u_long seldisptoolarge;        /* too much dispersion for selection */
  275.     u_long selbroken;        /* broken NTP detected in selection */
  276.     u_long seltooold;        /* too long since sync in selection */
  277.     u_char candidate;        /* position after candidate selection */
  278.     u_char select;            /* position at end of falseticker sel */
  279.     u_char was_sane;        /* set to 1 if it passed sanity check */
  280.     u_char correct;            /* set to 1 if it passed correctness check */
  281.     u_char last_event;        /* set to code for last peer error */
  282.     u_char num_events;        /* num. of events which have occurred */
  283. };
  284.  
  285. /*
  286.  * Values for peer.leap, sys_leap
  287.  */
  288. #define    LEAP_NOWARNING    0x0    /* normal, no leap second warning */
  289. #define    LEAP_ADDSECOND    0x1    /* last minute of day has 61 seconds */
  290. #define    LEAP_DELSECOND    0x2    /* last minute of day has 59 seconds */
  291. #define    LEAP_NOTINSYNC    0x3    /* overload, clock is free running */
  292.  
  293. /*
  294.  * Values for peer.mode
  295.  */
  296. #define    MODE_UNSPEC    0    /* unspecified (probably old NTP version) */
  297. #define    MODE_ACTIVE    1    /* symmetric active */
  298. #define    MODE_PASSIVE    2    /* symmetric passive */
  299. #define    MODE_CLIENT    3    /* client mode */
  300. #define    MODE_SERVER    4    /* server mode */
  301. #define    MODE_BROADCAST    5    /* broadcast mode */
  302. #define    MODE_CONTROL    6    /* control mode packet */
  303. #define    MODE_PRIVATE    7    /* implementation defined function */
  304.  
  305. #define    MODE_BCLIENT    8    /* a pseudo mode, used internally */
  306. #define MODE_MCLIENT    9    /* multicast mode, used internally */
  307.  
  308. /*
  309.  * Values for peer.stratum, sys_stratum
  310.  */
  311. #define    STRATUM_REFCLOCK ((u_char)0) /* stratum claimed by primary clock */
  312. #define    STRATUM_PRIMARY    ((u_char)1) /* host has a primary clock */
  313. #define    STRATUM_INFIN ((u_char)NTP_MAXSTRATUM) /* infinity a la Bellman-Ford */
  314. /* A stratum of 0 in the packet is mapped to 16 internally */
  315. #define    STRATUM_PKT_UNSPEC ((u_char)0) /* unspecified in packet */
  316. #define    STRATUM_UNSPEC    ((u_char)(NTP_MAXSTRATUM+(u_char)1)) /* unspecified */
  317.  
  318. /*
  319.  * Values for peer.flags
  320.  */
  321. #define    FLAG_CONFIG        0x1    /* association was configured */
  322. #define    FLAG_AUTHENABLE        0x2    /* this guy needs authentication */
  323. #define    FLAG_MCAST1        0x4    /* multicast client/server mode */
  324. #define    FLAG_MCAST2        0x8    /* multicast client mode */
  325. #define    FLAG_AUTHENTIC        0x10    /* last message was authentic */
  326. #define    FLAG_REFCLOCK        0x20    /* this is actually a reference clock */
  327. #define    FLAG_SYSPEER        0x40    /* this is one of the selected peers */
  328. #define FLAG_PREFER        0x80    /* this is the preferred peer */
  329.  
  330. /*
  331.  * Definitions for the clear() routine.  We use memset() to clear
  332.  * the parts of the peer structure which go to zero.  These are
  333.  * used to calculate the start address and length of the area.
  334.  */
  335. #define    CLEAR_TO_ZERO(p)    ((char *)&((p)->clear_to_zero))
  336. #define    END_CLEAR_TO_ZERO(p)    ((char *)&((p)->end_clear_to_zero))
  337. #define    LEN_CLEAR_TO_ZERO    (END_CLEAR_TO_ZERO((struct peer *)0) \
  338.                     - CLEAR_TO_ZERO((struct peer *)0))
  339. /*
  340.  * Reference clock identifiers (for pps signal)
  341.  */
  342. #define PPSREFID (u_int32)"PPS "        /* used when pps controls stratum > 1 */
  343.  
  344. /*
  345.  * Reference clock types.  Added as necessary.
  346.  */
  347. #define    REFCLK_NONE        0    /* unknown or missing */
  348. #define    REFCLK_LOCALCLOCK    1    /* external (e.g., lockclock) */
  349. #define    REFCLK_GPS_TRAK        2    /* TRAK 8810 GPS Receiver */
  350. #define    REFCLK_WWV_PST        3    /* PST/Traconex 1020 WWV/H */
  351. #define    REFCLK_WWVB_SPECTRACOM    4    /* Spectracom 8170/Netclock WWVB */
  352. #define    REFCLK_GOES_TRUETIME    5    /* TrueTime 468-DC GOES */
  353. #define REFCLK_IRIG_AUDIO    6       /* IRIG-B audio decoder */
  354. #define    REFCLK_CHU        7    /* scratchbuilt CHU (Canada) */
  355. #define REFCLK_PARSE        8    /* generic driver (usually DCF77,GPS,MSF) */
  356. #define    REFCLK_GPS_MX4200    9    /* Magnavox MX4200 GPS */
  357. #define REFCLK_GPS_AS2201    10    /* Austron 2201A GPS */
  358. #define    REFCLK_OMEGA_TRUETIME    11    /* TrueTime OM-DC OMEGA */
  359. #define REFCLK_IRIG_TPRO    12    /* KSI/Odetics TPRO-S IRIG */
  360. #define REFCLK_ATOM_LEITCH    13    /* Leitch CSD 5300 Master Clock */
  361. #define REFCLK_MSF_EES        14    /* EES M201 MSF Receiver */
  362. #define    REFCLK_GPSTM_TRUETIME    15    /* TrueTime GPS/TM-TMD Receiver */
  363. #define REFCLK_IRIG_BANCOMM    16    /* Bancomm GPS/IRIG Interface */
  364. #define REFCLK_GPS_DATUM    17    /* Datum Programmable Time System */
  365. #define REFCLK_NIST_ACTS    18    /* NIST Auto Computer Time Service */
  366. #define REFCLK_WWV_HEATH    19    /* Heath GC1000 WWV/WWVH Receiver */
  367. #define REFCLK_GPS_NMEA        20    /* NMEA based GPS clock */
  368. #define REFCLK_GPS_MOTO        21    /* Motorola GPS clock */
  369. #define REFCLK_ATOM_PPS        22    /* 1-PPS Clock Discipline */
  370. #define REFCLK_PTB_ACTS        23    /* PTB Auto Computer Time Service */
  371. #define REFCLK_USNO        24    /* Naval Observatory dialup */
  372. #define REFCLK_TRUETIME        25    /* TrueTime generic receiver */
  373. #define REFCLK_MAX        26    /* maximum index (room to expand) */
  374.  
  375. /*
  376.  * We tell reference clocks from real peers by giving the reference
  377.  * clocks an address of the form 127.127.t.u, where t is the type and
  378.  * u is the unit number.  We define some of this here since we will need
  379.  * some sanity checks to make sure this address isn't interpretted as
  380.  * that of a normal peer.
  381.  */
  382. #define    REFCLOCK_ADDR    0x7f7f0000    /* 127.127.0.0 */
  383. #define    REFCLOCK_MASK    0xffff0000    /* 255.255.0.0 */
  384.  
  385. #define    ISREFCLOCKADR(srcadr)    ((SRCADR(srcadr) & REFCLOCK_MASK) \
  386.                     == REFCLOCK_ADDR)
  387.  
  388. /*
  389.  * Macro for checking for invalid addresses.  This is really, really
  390.  * gross, but is needed so no one configures a host on net 127 now that
  391.  * we're encouraging it the the configuration file.
  392.  */
  393. #define    LOOPBACKADR    0x7f000001
  394. #define    LOOPNETMASK    0xff000000
  395.  
  396. #define    ISBADADR(srcadr)    (((SRCADR(srcadr) & LOOPNETMASK) \
  397.                     == (LOOPBACKADR & LOOPNETMASK)) \
  398.                     && (SRCADR(srcadr) != LOOPBACKADR))
  399.  
  400. /*
  401.  * Utilities for manipulating addresses and port numbers
  402.  */
  403. #define    NSRCADR(src)    ((src)->sin_addr.s_addr) /* address in net byte order */
  404. #define    NSRCPORT(src)    ((src)->sin_port)    /* port in net byte order */
  405. #define    SRCADR(src)    (ntohl(NSRCADR((src))))    /* address in host byte order */
  406. #define    SRCPORT(src)    (ntohs(NSRCPORT((src))))    /* host port */
  407.  
  408. /*
  409.  * NTP packet format.  The mac field is optional.  It isn't really
  410.  * an l_fp either, but for now declaring it that way is convenient.
  411.  * See Appendix A in the specification.
  412.  *
  413.  * Note that all u_fp and l_fp values arrive in network byte order
  414.  * and must be converted (except the mac, which isn't, really).
  415.  */
  416. struct pkt {
  417.     u_char li_vn_mode;    /* contains leap indicator, version and mode */
  418.     u_char stratum;        /* peer's stratum */
  419.     u_char ppoll;        /* the peer polling interval */
  420.     s_char precision;    /* peer clock precision */
  421.     s_fp rootdelay;        /* distance to primary clock */
  422.     u_fp rootdispersion;    /* clock dispersion */
  423.     u_int32 refid;        /* reference clock ID */
  424.     l_fp reftime;        /* time peer clock was last updated */
  425.     l_fp org;        /* originate time stamp */
  426.     l_fp rec;        /* receive time stamp */
  427.     l_fp xmt;        /* transmit time stamp */
  428.  
  429. #define    MIN_MAC_LEN    (sizeof(u_int32) + 8)        /* DES */
  430. #define    MAX_MAC_LEN    (sizeof(u_int32) + 16)        /* MD5 */
  431.  
  432.     u_int32 keyid;        /* key identification */
  433.     u_char mac[MAX_MAC_LEN-sizeof(u_int32)];/* message-authentication code */
  434.     /*l_fp mac;*/
  435. };
  436.  
  437. /*
  438.  * Packets can come in two flavours, one with a mac and one without.
  439.  */
  440. #define    LEN_PKT_NOMAC    (sizeof(struct pkt) - MAX_MAC_LEN)
  441.  
  442. /*
  443.  * Minimum size of packet with a MAC: has to include at least a key number.
  444.  */
  445. #define    LEN_PKT_MAC    (LEN_PKT_NOMAC + sizeof(u_int32))
  446.  
  447. /*
  448.  * Stuff for extracting things from li_vn_mode
  449.  */
  450. #define    PKT_MODE(li_vn_mode)    ((u_char)((li_vn_mode) & 0x7))
  451. #define    PKT_VERSION(li_vn_mode)    ((u_char)(((li_vn_mode) >> 3) & 0x7))
  452. #define    PKT_LEAP(li_vn_mode)    ((u_char)(((li_vn_mode) >> 6) & 0x3))
  453.  
  454. /*
  455.  * Stuff for putting things back into li_vn_mode
  456.  */
  457. #define    PKT_LI_VN_MODE(li, vn, md) \
  458.     ((u_char)((((li) << 6) & 0xc0) | (((vn) << 3) & 0x38) | ((md) & 0x7)))
  459.  
  460.  
  461. /*
  462.  * Dealing with stratum.  0 gets mapped to 16 incoming, and back to 0
  463.  * on output.
  464.  */
  465. #define    PKT_TO_STRATUM(s)    ((u_char)(((s) == (STRATUM_PKT_UNSPEC)) ?\
  466.                 (STRATUM_UNSPEC) : (s)))
  467.  
  468. #define    STRATUM_TO_PKT(s)    ((u_char)(((s) == (STRATUM_UNSPEC)) ?\
  469.                 (STRATUM_PKT_UNSPEC) : (s)))
  470.  
  471. /*
  472.  * Format of a recvbuf.  These are used by the asynchronous receive
  473.  * routine to store incoming packets and related information.
  474.  */
  475.  
  476. /*
  477.  *  the maximum length NTP packet is a full length NTP control message with
  478.  *  the maximum length message authenticator.  I hate to hard-code 468 and 12,
  479.  *  but only a few modules include ntp_control.h...
  480.  */   
  481. #define    RX_BUFF_SIZE    (468+12+MAX_MAC_LEN)
  482.  
  483. struct recvbuf {
  484.     struct recvbuf *next;        /* next buffer in chain */
  485.     union {
  486.         struct sockaddr_in X_recv_srcadr;
  487.         caddr_t X_recv_srcclock;
  488.     } X_from_where;
  489. #define recv_srcadr    X_from_where.X_recv_srcadr
  490. #define    recv_srcclock    X_from_where.X_recv_srcclock
  491.     struct sockaddr_in srcadr;    /* where packet came from */
  492.     struct interface *dstadr;    /* interface datagram arrived thru */
  493.     int fd;                /* fd on which it was received */
  494.     l_fp recv_time;            /* time of arrival */
  495.     void (*receiver)();        /* routine to receive buffer */
  496.     int recv_length;        /* number of octets received */
  497.     union {
  498.         struct pkt X_recv_pkt;
  499.         char X_recv_buffer[RX_BUFF_SIZE];
  500.     } recv_space;
  501. #define    recv_pkt    recv_space.X_recv_pkt
  502. #define    recv_buffer    recv_space.X_recv_buffer
  503. };
  504.  
  505.  
  506. /*
  507.  * Event codes.  Used for reporting errors/events to the control module
  508.  */
  509. #define    PEER_EVENT    0x80        /* this is a peer event */
  510.  
  511. #define    EVNT_UNSPEC    0
  512. #define    EVNT_SYSRESTART    1
  513. #define    EVNT_SYSFAULT    2
  514. #define    EVNT_SYNCCHG    3
  515. #define    EVNT_PEERSTCHG    4
  516. #define    EVNT_CLOCKRESET    5
  517. #define    EVNT_BADDATETIM    6
  518. #define    EVNT_CLOCKEXCPT    7
  519.  
  520. #define    EVNT_PEERIPERR    (1|PEER_EVENT)
  521. #define    EVNT_PEERAUTH    (2|PEER_EVENT)
  522. #define    EVNT_UNREACH    (3|PEER_EVENT)
  523. #define    EVNT_REACH    (4|PEER_EVENT)
  524. #define    EVNT_PEERCLOCK    (5|PEER_EVENT)
  525.  
  526. /*
  527.  * Clock event codes
  528.  */
  529. #define    CEVNT_NOMINAL    0
  530. #define    CEVNT_TIMEOUT    1
  531. #define    CEVNT_BADREPLY    2
  532. #define    CEVNT_FAULT    3
  533. #define    CEVNT_PROP    4
  534. #define    CEVNT_BADDATE    5
  535. #define    CEVNT_BADTIME    6
  536. #define CEVNT_MAX    CEVNT_BADTIME
  537.  
  538. /*
  539.  * Very misplaced value.  Default port through which we send traps.
  540.  */
  541. #define    TRAPPORT    18447
  542.  
  543.  
  544. /*
  545.  * To speed lookups, peers are hashed by the low order bits of the remote
  546.  * IP address.  These definitions relate to that.
  547.  */
  548. #define    HASH_SIZE    32
  549. #define    HASH_MASK    (HASH_SIZE-1)
  550. #define    HASH_ADDR(src)    ((SRCADR((src))^(SRCADR((src))>>8)) & HASH_MASK)
  551.  
  552.  
  553. /*
  554.  * The poll update procedure takes an extra argument which controls
  555.  * how a random perturbation is applied to peer.timer.  The choice is
  556.  * to not randomize at all, to randomize only if we're going to update
  557.  * peer.timer, and to randomize no matter what (almost, the algorithm
  558.  * is that we apply the random value if it is less than the current
  559.  * timer count).
  560.  */
  561. #define    POLL_NOTRANDOM        0    /* don't randomize */
  562. #define    POLL_RANDOMCHANGE    1    /* if you change, change randomly */
  563. #define    POLL_MAKERANDOM        2    /* randomize next interval */
  564.  
  565.  
  566. /*
  567.  * How we randomize polls.  The poll interval is a power of two.
  568.  * We chose a random value which is between 1/4 and 3/4 of the
  569.  * poll interval we would normally use and which is an even multiple
  570.  * of the EVENT_TIMEOUT.  The random number routine, given an argument
  571.  * spread value of n, returns an integer between 0 and (1<<n)-1.  This
  572.  * is shifted by EVENT_TIMEOUT and added to the base value.
  573.  */
  574. #define    RANDOM_SPREAD(poll)    ((poll) - (EVENT_TIMEOUT+1))
  575. #define    RANDOM_POLL(poll, rval)    ((((rval)+1)<<EVENT_TIMEOUT) + (1<<((poll)-2)))
  576.  
  577. /*
  578.  * min, min3 and max.  Makes it easier to transliterate the spec without
  579.  * thinking about it.
  580.  */
  581. #define    min(a,b)    (((a) < (b)) ? (a) : (b))
  582. #define    max(a,b)    (((a) > (b)) ? (a) : (b))
  583. #define    min3(a,b,c)    min(min((a),(b)), (c))
  584.  
  585.  
  586. /*
  587.  * Configuration items.  These are for the protocol module (proto_config())
  588.  */
  589. #define    PROTO_BROADCLIENT    1
  590. #define    PROTO_PRECISION        2
  591. #define    PROTO_AUTHENTICATE    3
  592. #define    PROTO_BROADDELAY    4
  593. #define    PROTO_AUTHDELAY        5
  594. #define PROTO_MULTICAST_ADD    6
  595. #define PROTO_MULTICAST_DEL    7
  596. #define PROTO_PLL        8
  597. #define PROTO_PPS        9
  598. #define PROTO_MONITOR        10
  599. #define PROTO_FILEGEN        11
  600.  
  601. /*
  602.  * Configuration items for the loop filter
  603.  */
  604. #define    LOOP_DRIFTCOMP        1    /* set frequency offset */
  605. #define LOOP_PPSDELAY        2    /* set pps delay */
  606. #define LOOP_PPSBAUD        3    /* set pps baud rate */
  607.  
  608. /*
  609.  * Configuration items for the stats printer
  610.  */
  611. #define    STATS_FREQ_FILE        1    /* configure drift file */
  612. #define STATS_STATSDIR        2    /* directory prefix for stats files */
  613. #define    STATS_PID_FILE        3    /* configure xntpd PID file */
  614.  
  615. #define MJD_1970        40587    /* MJD for 1 Jan 1970 */
  616.  
  617. /*
  618.  * Default parameters.  We use these in the absense of something better.
  619.  */
  620. #define    DEFPRECISION    (-7)        /* default precision (~10 ms) */
  621. #define    DEFBROADDELAY    0x00000100    /* default broadcast offset */
  622.                     /* (~4 ms as s_fp) */
  623. #define DEFAUTHDELAY    0x00080000    /* default authentcation delay */
  624.                     /* (~100 us as l_fp.u_f) */
  625. #define INADDR_NTP    0xe0000101    /* NTP multicast address 224.0.1.1 */
  626. /*
  627.  * Structure used optionally for monitoring when this is turned on.
  628.  */
  629. struct mon_data {
  630.     struct mon_data *hash_next;    /* next structure in hash list */
  631.     struct mon_data *mru_next;    /* next structure in MRU list */
  632.     struct mon_data *mru_prev;    /* previous structure in MRU list */
  633.     struct mon_data *fifo_next;    /* next structure in FIFO list */
  634.     struct mon_data *fifo_prev;    /* previous structure in FIFO list */
  635.     u_long lastdrop;        /* last time dropped due to RES_LIMIT*/
  636.     u_long lasttime;        /* last time data updated */
  637.     u_long firsttime;        /* time structure initialized */
  638.     u_long count;            /* count we have seen */
  639.     u_int32 rmtadr;            /* address of remote host */
  640.     struct interface *interface;    /* interface on which this arrived */
  641.     u_short rmtport;        /* remote port last came from */
  642.     u_char mode;            /* mode of incoming packet */
  643.     u_char version;            /* version of incoming packet */
  644.     u_char cast_flags;        /* flags MDF_?CAST */
  645. };
  646.  
  647. #define    MDF_UCAST    1        /* unicast packet */
  648. #define    MDF_MCAST    2        /* multicast packet */
  649. #define    MDF_BCAST    4        /* broadcast packet */
  650. #define    MDF_LCAST    8        /* local packet */
  651.  
  652. /*
  653.  * Values used with mon_enabled to indicate reason for enabling monitoring
  654.  */
  655. #define MON_OFF    0x00            /* no monitoring */
  656. #define MON_ON     0x01            /* monitoring explicitly enabled */
  657. #define MON_RES    0x02            /* implicit monitoring for RES_LIMITED */
  658. /*
  659.  * Structure used for restrictlist entries
  660.  */
  661. struct restrictlist {
  662.     struct restrictlist *next;    /* link to next entry */
  663.     u_int32 addr;            /* host address (host byte order) */
  664.     u_int32 mask;            /* mask for address (host byte order) */
  665.     u_long count;            /* number of packets matched */
  666.     u_short flags;            /* accesslist flags */
  667.     u_short mflags;            /* match flags */
  668. };
  669.  
  670. /*
  671.  * Access flags
  672.  */
  673. #define    RES_IGNORE        0x1    /* ignore if matched */
  674. #define    RES_DONTSERVE        0x2    /* don't give him any time */
  675. #define    RES_DONTTRUST        0x4    /* don't trust if matched */
  676. #define    RES_NOQUERY        0x8    /* don't allow queries if matched */
  677. #define    RES_NOMODIFY        0x10    /* don't allow him to modify server */
  678. #define    RES_NOPEER        0x20    /* don't allocate memory resources */
  679. #define    RES_NOTRAP        0x40    /* don't allow him to set traps */
  680. #define    RES_LPTRAP        0x80    /* traps set by him are low priority */
  681. #define RES_LIMITED        0x100   /* limit per net number of clients */
  682.  
  683. #define    RES_ALLFLAGS \
  684.     (RES_IGNORE|RES_DONTSERVE|RES_DONTTRUST|RES_NOQUERY\
  685.     |RES_NOMODIFY|RES_NOPEER|RES_NOTRAP|RES_LPTRAP|RES_LIMITED)
  686.  
  687. /*
  688.  * Match flags
  689.  */
  690. #define    RESM_INTERFACE        0x1    /* this is an interface */
  691. #define    RESM_NTPONLY        0x2    /* match ntp port only */
  692.  
  693. /*
  694.  * Restriction configuration ops
  695.  */
  696. #define    RESTRICT_FLAGS        1    /* add flags to restrict entry */
  697. #define    RESTRICT_UNFLAG        2    /* remove flags from restrict entry */
  698. #define    RESTRICT_REMOVE        3    /* remove a restrict entry */
  699.  
  700.  
  701. /*
  702.  * Experimental alternate selection algorithm identifiers
  703.  */
  704. #define    SELECT_1    1
  705. #define    SELECT_2    2
  706. #define    SELECT_3    3
  707. #define    SELECT_4    4
  708. #define    SELECT_5    5
  709.  
  710. /*
  711.  * Endpoint structure for the select algorithm
  712.  */
  713. struct endpoint {
  714.     s_fp    val;            /* offset of endpoint */
  715.     int    type;            /* interval entry/exit */
  716. };
  717.